package com.skycong.hello77.activity.mydialogcar;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.skycong.hello77.R;


/**
 * 加载中的小车
 * Created by Kiki on 2018/5/2.
 */

public class MyProgressDialog_car extends Dialog {
    private Context context;//上下文内容
    private RelativeLayout relativeLayout;
    private ImageView img;//加载中小车的图片
    private TextView tipText;//加载中的提示词
    private AnimationDrawable animationDrawable;//帧动画
    /*
    * Alpha——淡入淡出，Scale——缩放效果，Rotate——旋转，Translate——移动效果。
    * */
    private Animation animation;//画效果的API,

    public MyProgressDialog_car(Context context) {
        super(context, R.style.progress_dialog);
        this.context = context;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //加载布局文件
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//获取系统服务
        View view = inflater.inflate(R.layout.progress_dialog_car, null);
        /*
        * Android提供的记述屏幕的有关信息的一种结构，诸如其尺寸，密度和字体缩放的一般信息。
        * */
        DisplayMetrics dm = new DisplayMetrics();//获取屏幕的大小
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);

        relativeLayout = (RelativeLayout) view.findViewById(R.id.home_loading_layout);
        ViewGroup.LayoutParams mParams = relativeLayout.getLayoutParams();//设置参数
        mParams.width = dm.widthPixels * 2 / 3;

        img = (ImageView) view.findViewById(R.id.progress_dialog_img);
        animation = AnimationUtils.loadAnimation(context, R.anim.loading_dialog_progressbar);//加载动画

        tipText = (TextView) view.findViewById(R.id.progress_dialog_txt);
        relativeLayout.setBackgroundColor(Color.argb(0, 0, 0, 0));
        tipText.setVisibility(View.GONE);
        //dialog添加视图
        setCanceledOnTouchOutside(false);//dialog弹出后会点击屏幕，dialog不消失；点击物理返回键dialog消失
        setContentView(view);

    }

    //设置提示字体
    public void setMsg(String msg) {
        tipText.setText(msg);
    }

    public void setMsg(int msgId) {
        tipText.setText(msgId);
    }

    /**
     * 物理返回键返回
     */
    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (isShowing()) {
            hide();
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 加载中的小车显示
     */
    @Override
    public void show() {
        if (animation != null) {
            img.setAnimation(animation);
        }
        super.show();
    }

    //设置提示字的显隐，有背景颜色
    public void displayTipTextshow() {
        relativeLayout.setBackgroundResource(R.drawable.defalute_dialog_car);
        tipText.setVisibility(View.VISIBLE);
        super.show();

    }

    //设置字体的显影，没有背景颜色的
    public void displayTipTextshowWithOut() {
        tipText.setVisibility(View.VISIBLE);
        tipText.setTextColor(Color.BLACK);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (animation != null) {
            img.clearAnimation();
        }
    }

    /**
     * 设置dialog的隐藏
     */
    @Override
    public void hide() {
        dismiss();
        super.hide();
        if (Build.VERSION.SDK_INT >= 24) {
            super.dismiss();
        } else {
            super.hide();
        }
    }
}
